package com.onpositive.wiki3.db.catmodel.matches.classify;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

import com.onpositive.nlp.inflector.SimplePOFParser;
import com.onpositive.wiki3.db.catmodel.Categories;
import com.onpositive.wiki3.db.catmodel.Category;

public class YearMatcher extends AbstractRecordingMatcher {

	Categories all;

	public YearMatcher(RelationStore ss, Categories all) {
		super(ss);
		this.all = all;
	}

	public static HashSet<String> tags = new HashSet<String>();

	static {
		tags.add("deaths");
		// tags.add("works");
		tags.add("births");
		tags.add("years");
		tags.add("articles");
		tags.add("centuries");
		// tags.add("disestablishments");//establishments
		// tags.add("establishments");
	}

	@Override
	public int accept(Category st) {
		String string = st.toString();
		String core = SimplePOFParser.getCore(string);
		String[] split = core.split("_");
		if (split[0].length() == 0) {
			return 0;
		}
		if (Character.isDigit(split[0].charAt(0))) {
			try {

				if (split[0].endsWith("s")) {
					split[0] = split[0].substring(0, split[0].length() - 1);
				}
				if (split[0].endsWith("th")) {
					split[0] = split[0].substring(0, split[0].length() - 2);
				}
				if (split[0].endsWith("th-century")) {
					split[0] = split[0].substring(0, split[0].length()
							- "th-century".length());
				}
				Integer.parseInt(split[0]);

				if (split.length == 1) {
					mark(RelationConstants.GARBAGE, st);
				} else {

					ArrayList<String> sm = new ArrayList<String>(
							Arrays.asList(split));
					sm.remove("century");
					sm.remove("bc");
					if (sm.size() > 2) {
						String string2 = sm.get(sm.size() - 2);
						if (string2.equals("in")||string2.equals("of")) {
							String mm=sm.get(sm.size()-1);
							//TODO check if it is a place;
							//System.out.println(mm);
						}
					}
					String[] split2 = sm.toArray(new String[sm.size()]);
					if (split2.length == 1) {
						mark(RelationConstants.GARBAGE, st);
						return 0;
					}
					if (split2.length == 2) {
						Category category = all.get(split2[1]);
						if (category != null) {
							record(RelationConstants.IS_A, category, st);
							Category[] childCategories = st
									.getChildCategories();
							for (Category c : childCategories) {
								record(RelationConstants.IS_A, category, c);
							}
							return 0;
						} else {
							if (tags.contains(split2[1])) {
								mark(RelationConstants.GARBAGE, st);
								Category[] childCategories = st
										.getChildCategories();
								Category cm = all.get("people");
								for (Category c : childCategories) {
									record(RelationConstants.IS_A, cm, c);
								}
								return 0;
							}
						}
					}
					// System.out.println(string);
				}
			} catch (NumberFormatException e) {
				// System.out.println(string);
				// TODO: handle exception
			}
		}

		return 0;
	}

}
